﻿#light

module PubSim.Output

open System
open System.IO
open System.Threading
open System.Collections
open System.Collections.Generic
open Microsoft.FSharp.Control
open System.Configuration

type Output() =
    let fileName = @"log.txt"
    let logMethod = ConfigurationManager.AppSettings.Item("LogMethod")
    let WriteToFile(message : string) =
        using (new StreamWriter(fileName, true)) (fun outp ->
            outp.WriteLine(message))
    
    member g.Start(outputQueue : Queue<string>) =
        if System.IO.File.Exists(fileName) then
            System.IO.File.Delete(fileName)
            
        while true do
            while (outputQueue.Count > 0) do
                let s = outputQueue.Dequeue()
                match logMethod with
                | "console" -> printfn "%s -- %s" (DateTime.Now.ToString("hh:mm:ss.fff")) s
                | "file"    -> WriteToFile(DateTime.Now.ToString("hh:mm:ss.fff -- ") + s)
                | _         -> printfn "Not a valid chose for logMethod"
            Thread.Sleep(50)
